package com.jinyu.gateway;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

@SpringBootApplication
@EnableConfigurationProperties

@Slf4j
public class GatewayApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
//        NioEventLoopGroup bossGroup = new NioEventLoopGroup(2, new ThreadFactory() {
//            @Override
//            public Thread newThread(Runnable r) {
//                return new Thread(r, "boss-" + System.currentTimeMillis());
//            }
//        });
//        NioEventLoopGroup workGroup = new NioEventLoopGroup(2, new ThreadFactory() {
//            @Override
//            public Thread newThread(Runnable r) {
//                return new Thread(r, "work-" + System.currentTimeMillis());
//            }
//        });
//        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
//            log.info("应用服务已关闭");
//            bossGroup.shutdownGracefully();
//            workGroup.shutdownGracefully();
//        }));
//
//        try {
//            ServerBootstrap serverBootstrap = new ServerBootstrap();
//            serverBootstrap.group(bossGroup, workGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .option(ChannelOption.SO_BACKLOG, 100)
//                    .handler(new LoggingHandler(LogLevel.DEBUG))
//                    .childHandler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            ChannelPipeline pipeline = socketChannel.pipeline();
//                            pipeline.addLast(new LoggingHandler(LogLevel.INFO));
//                            pipeline.addLast(new BusinessHandler());
//                        }
//                    });
//            ChannelFuture sync = serverBootstrap.bind(9999).sync();
////            sync.channel().closeFuture().sync();
//            sync.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> {
//                log.info("received the server close request");
//            });
//        } finally {
//
//        }
//
////        init(2);
    }

    private static String HOST = "127.0.0.1";
    private static int PORT = 9999;

    public void init(int poolSize) throws Exception {
        for (int i = 0; i < poolSize; i++) {
            EventLoopGroup group = new NioEventLoopGroup();
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .option(ChannelOption.SO_KEEPALIVE, true)
                    .option(ChannelOption.SO_RCVBUF, 1024 * 1024)
                    .option(ChannelOption.SO_SNDBUF, 1024 * 1024)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO));
                        }
                    });
            ChannelFuture sync = b.connect(HOST, PORT).sync();
            sync.channel().closeFuture().addListener((r) -> {
                group.shutdownGracefully();
            });
        }
    }
}
